package com.nisco.dms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.Department;
import com.nisco.dms.exception.ExistedException;
import com.nisco.dms.service.DeptService;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;


@Controller
@RequestMapping(value = "/dept")
public class DeptController {
	@Autowired
	private DeptService deptService;
	private static final String CREATE = "dept/deptCreate";
	private static final String UPDATE = "dept/deptUpdate";
	private static final String LIST = "dept/deptList";

	@RequestMapping(value = "/list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String list(Page page, String keywords,Map<String, Object> map) throws Exception {
		int maxSize = 0;
		if (StringUtils.isNotBlank(keywords)) {
			maxSize = deptService.search(keywords).size();
		}else{
			maxSize = deptService.getAll().size();
		}
		page.setTotalCount(maxSize);
		List<Department> depts = new ArrayList<Department>();
		if (StringUtils.isNotBlank(keywords)) {
			depts.addAll(deptService.search(keywords,(page.getPageNum()!=1?page.getPageNum()-1:0)*page.getNumPerPage(), page.getNumPerPage()));
		} else {
			depts = deptService.getAllByPage((page.getPageNum()!=1?page.getPageNum()-1:0)*page.getNumPerPage(), page.getNumPerPage());
		}
		map.put("page", page);
		map.put("depts", depts);
		map.put("keywords", keywords);
		return LIST;
	}

	@ModelAttribute("preloadDept")
	public Department getOne(@RequestParam(value = "id", required = false) String id) {
		if (id != null) {
			return deptService.get(id);
		}
		return null;
	}

	@RequiresPermissions("Dept:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate() {
		return CREATE;
	}

	@RequiresPermissions("Dept:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String Create(Department dept) {
		deptService.save(dept);
		AjaxObject ajaxObject = new AjaxObject(200,"部门添加成功！","moduleListNav_14","","","closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("Dept:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String id, Map<String, Object> map) {
		Department dept = deptService.get(id);
		map.put("dept", dept);
		return UPDATE;
	}

	@RequiresPermissions("Dept:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(@ModelAttribute("preloadDept") Department dept) {
		deptService.update(dept);

		AjaxObject ajaxObject = new AjaxObject(200,"部门修改成功！","moduleListNav_14","","","closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("Dept:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable String id) {
		AjaxObject ajaxObject = new AjaxObject("部门删除成功！");
		try {
			deptService.delete(id);
		} catch (ExistedException e) {
			ajaxObject.setMessage(e.getMessage());
		}

		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}
	
	@RequiresPermissions("Dept:exportXls")
	@RequestMapping(value = "/createXls", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String createXls(ModelMap mm){
		List<Department> depts = deptService.getAll();
		mm.addAttribute("depts", depts);
		return "deptListExcel";
	}
}
